package servlet.order;


import bean.dto.OrderDto;
import bean.dto.OrderDto2;
import bean.pojo.Medical;
import bean.vo.OrderVo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.OrderService;
import service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.order
 * User: 李雨行
 * Date: 2024/12/18 10:33
 * motto:   沉淀哥
 * Description:
 * Version: V1.0
 */
@WebServlet(value = "/order/*")
public class OrderServlet extends BaseServlet {
    public R findByPage(HttpServletRequest request){
        Integer orderId= NumberFormat.stringToInteger(request.getParameter("orderId"));
        Integer page= NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit= NumberFormat.stringToInteger(request.getParameter("limit"));
        String userName=request.getParameter("userName");
        String productName=request.getParameter("productName");
        OrderService orderService=new OrderServiceImpl();
        List<OrderVo> list = orderService.findByPage(orderId,userName,productName,page,limit);
        Integer maxPage=orderService.maxPage(orderId,userName,productName);
        return list.size()>0?R.ok().addData("total",maxPage).addData("data",list):R.error("没有数据");
    }

    public R add(HttpServletRequest request){
        try {
            BufferedReader bufferedReader =request.getReader();
            OrderDto orderDto= JSONObject.parseObject(bufferedReader.readLine(), OrderDto.class);
            OrderService orderService=new OrderServiceImpl();
            return orderService.add(orderDto)?R.ok("添加成功"):R.ok("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("添加出现问题");
    }

    public R deleteById(HttpServletRequest request){
        Integer orderId= NumberFormat.stringToInteger(request.getParameter("orderId"));
        OrderService orderService=new OrderServiceImpl();
        return orderService.deleteById(orderId)?R.ok("删除成功"):R.ok("删除失败");

    }

    public R add2(HttpServletRequest request){
        try {
            BufferedReader bufferedReader =request.getReader();
            OrderDto2 orderDto2= JSONObject.parseObject(bufferedReader.readLine(), OrderDto2.class);
            OrderService orderService=new OrderServiceImpl();
            return orderService.add2(orderDto2)?R.ok("添加成功"):R.ok("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("添加出现问题");
    }
    public R change(HttpServletRequest request){
        Integer orderId=NumberFormat.stringToInteger(request.getParameter("orderId"));
        OrderService orderService=new OrderServiceImpl();
        boolean t=orderService.change(orderId);
        return t?R.ok("状态修改成功"):R.ok("状态修改失败");
    }
}
